/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managers;

/**
 *
 * @author juan
 */
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

@ManagedBean
@SessionScoped
public class managerAutoridad implements Serializable {

    private TreeNode root;
    private TreeNode[] selectedNodes;
    Integer perfilSeleccionado;
    String nombrePerfil;
    //String perfilSeleccionado = "2";

    public String getNombrePerfil() {
        return nombrePerfil;
    }

    public void setNombrePerfil(String nombrePerfil) {
        this.nombrePerfil = nombrePerfil;
    }
    WebApplicationContext permisos = FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
    JdbcTemplate jdbcTemplate = (JdbcTemplate) permisos.getBean("jdbcTemplate");

    public managerAutoridad() {
        //this.perfilSeleccionado = "";

        root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("SOLICITUD", root);
        TreeNode node1 = new DefaultTreeNode("EXPEDIENTE", root);
        TreeNode node2 = new DefaultTreeNode("ESTADISTICAS", root);
        TreeNode node3 = new DefaultTreeNode("PERFIL", root);
        TreeNode node4 = new DefaultTreeNode("USUARIO", root);
        TreeNode node5 = new DefaultTreeNode("MATRIZ DE RECORRIDOS", root);
        //TreeNode node6 = new DefaultTreeNode("REPORTE", root);
        TreeNode node7 = new DefaultTreeNode("MANTENIMIENTO", root);
        TreeNode node8 = new DefaultTreeNode("AUDITORIA", root);

        TreeNode node00 = new DefaultTreeNode("CREAR SOLICITUD", node0);
        TreeNode node01 = new DefaultTreeNode("MODIFICAR SOLICITUD", node0);
        TreeNode node02 = new DefaultTreeNode("CONSULTAR SOLICITUD", node0);

        TreeNode node10 = new DefaultTreeNode("CREAR EXPEDIENTE", node1);
        TreeNode node11 = new DefaultTreeNode("MODIFICAR EXPEDIENTE", node1);
        TreeNode node12 = new DefaultTreeNode("CONSULTAR EXPEDIENTE", node1);
        TreeNode node13 = new DefaultTreeNode("CONTROL DE FECHAS", node1);
        TreeNode node14 = new DefaultTreeNode("ASIGNAR ESTADO EXPEDIENTE", node1);
        TreeNode node15 = new DefaultTreeNode("CREAR REPORTE", node1);
        TreeNode node16 = new DefaultTreeNode("ASIGNAR VISTO BUENO", node1);

        TreeNode node20 = new DefaultTreeNode("CONSULTAR ESTADISTICA", node2);

        TreeNode node30 = new DefaultTreeNode("CREAR PERFIL", node3);
        TreeNode node31 = new DefaultTreeNode("MODIFICAR PERFIL", node3);
        TreeNode node32 = new DefaultTreeNode("CONSULTAR PERFIL", node3);

        TreeNode node40 = new DefaultTreeNode("CREAR USUARIO", node4);
        TreeNode node41 = new DefaultTreeNode("MODIFICAR DATOS PERSONALES", node4);
        TreeNode node42 = new DefaultTreeNode("MODIFICAR PERFIL USUARIO", node4);
        TreeNode node43 = new DefaultTreeNode("ESTADO DE USUARIO", node4);

        TreeNode node50 = new DefaultTreeNode("CREAR MATRIZ DE RECORRIDOS", node5);

        TreeNode node70 = new DefaultTreeNode("MODIFICAR MANTENIMIENTO", node7);
        TreeNode node80 = new DefaultTreeNode("CONSULTAR AUDITORIA", node8);

    }

    public TreeNode getRoot() {
        return root;
    }

    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }

    public void displaySelectedMultiple(ActionEvent event) {
        add();
        borrar();
        entrar_sql("ROLE_PASO");
        StringBuilder builder = new StringBuilder();

        for (TreeNode node : selectedNodes) {
            insertar_roles(node.getData().toString());
            builder.append(node.getData().toString());
            builder.append("<br />");
        }

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Permisos Asignados:", builder.toString());
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void insertar_roles(String x) {
        String a = "CREAR EXPEDIENTE";
        String b = " ";
        if (a.equals(x)) {
            a = "ROLE_CREAREXPE";
            b = "CREAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_MODEXPE";
            b = "MODIFICAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "CONSULTAR EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_CONSUEXPE";
            b = "CONSULTAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "CREAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_CREASOLI";
            b = "CREAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "MODIFICAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_MODSOLI";
            b = "MODIFICAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "CONSULTAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_CONSUSOLI";
            b = "CONSULTAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "CONTROL DE FECHAS";
        if (a.equals(x)) {
            a = "ROLE_CONTROLFECHA";
            b = "CONTROL DE FECHAS";
            insert_sql(a, b);
        }
        a = "CONSULTAR ESTADISTICA";
        if (a.equals(x)) {
            a = "ROLE_CONSUESTA";
            b = "CONSULTAR ESTADISTICA";
            insert_sql(a, b);
        }
        a = "CREAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_CREARPERF";
            b = "CREAR PERFIL";
            insert_sql(a, b);
        }
        a = "MODIFICAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_MODPERF";
            b = "MODIFICAR PERFIL";
            insert_sql(a, b);
        }
        a = "CONSULTAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_CONPERF";
            b = "CONSULTAR PERFIL";
            insert_sql(a, b);
        }
        a = "CREAR USUARIO";
        if (a.equals(x)) {
            a = "ROLE_CREUSUA";
            b = "CREAR USUARIO";
            insert_sql(a, b);
        }
        a = "MODIFICAR DATOS PERSONALES";
        if (a.equals(x)) {
            a = "ROLE_MODUSUA";
            b = "MODIFICAR DATOS PERSONALES";
            insert_sql(a, b);
        }
         a = "MODIFICAR PERFIL USUARIO";
        if (a.equals(x)) {
            a = "ROLE_MODPERUSU";
            b = "MODIFICAR PERFIL USUARIO";
            insert_sql(a, b);
        }
        a = "ESTADO DE USUARIO";
        if (a.equals(x)) {
            a = "ROLE_MODESTAUSU";
            b = "ESTADO DE USUARIO";
            insert_sql(a, b);
        }
        a = "CONSULTAR USUARIO";
        if (a.equals(x)) {
            a = "ROLE_CONSUSUA";
            b = "CONSULTAR USUARIO";
            insert_sql(a, b);
        }
        a = "CREAR MATRIZ DE RECORRIDOS";
        if (a.equals(x)) {
            a = "ROLE_CREARMATRIZ";
            b = "CREAR MATRIZ DE RECORRIDOS";
            insert_sql(a, b);
        }
        a = "CONSULTAR MATRIZ DE RECORRIDOS";
        if (a.equals(x)) {
            a = "ROLE_CONSMATRIZ";
            b = "CONSULTAR MATRIZ DE RECORRIDOS";
            insert_sql(a, b);
        }
        a = "CREAR REPORTE";
        if (a.equals(x)) {
            a = "ROLE_CREAREPO";
            b = "CREAR REPORTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR REPORTE";
        if (a.equals(x)) {
            a = "ROLE_MODREPO";
            b = "MODIFICAR REPORTE";
            insert_sql(a, b);
        }
        a = "ASIGNAR ESTADO EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_ESTADOEXPE";
            b = "ASIGNAR ESTADO EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR MANTENIMIENTO";
        if (a.equals(x)) {
            a = "ROLE_MANTO";
            b = "MODIFICAR MANTENIMIENTO";
            insert_sql(a, b);
        }
        a = "CONSULTAR AUDITORIA";
        if (a.equals(x)) {
            a = "ROLE_AUDIO";
            b = "CONSULTAR AUDITORIA";
            insert_sql(a, b);
        }
        a = "ASIGNAR VISTO BUENO";
        if (a.equals(x)) {
            a = "ROLE_VISTOB";
            b = "ASIGNAR VISTO BUENO";
            insert_sql(a, b);
        }

    }

    public void borrar() {
        String sql = "";
        sql = "DELETE FROM PERFIL_AUTORIDAD WHERE idperfil = " + this.perfilSeleccionado + " ";
        jdbcTemplate.execute(sql);
    }

    public void insert_sql(String s, String z) {
        String sql = "";
        //System.out.println(this.perfilSeleccionado);
        sql = "INSERT INTO PERFIL_AUTORIDAD VALUES (nextval('perfil_autoridadseq')," + this.perfilSeleccionado + ",'" + s + "','" + z + "')";
        jdbcTemplate.execute(sql);
    }

    public void entrar_sql(String s) {
        String sql = "";
        //System.out.println(this.perfilSeleccionado);
        sql = "INSERT INTO PERFIL_AUTORIDAD VALUES (nextval('perfil_autoridadseq')," + this.perfilSeleccionado + ",'" + s + "')";
        jdbcTemplate.execute(sql);
    }

    public void entrar(String s) {
        String sql = "";
        //System.out.println(this.perfilSeleccionado);
        sql = "INSERT INTO PERFIL_AUTORIDAD VALUES (nextval('perfil_autoridadseq'),(select idperfil from perfil where nombreperfil = upper('" + this.nombrePerfil + "')),'" + s + "')";
        jdbcTemplate.execute(sql);
    }

    public void add() {
        String sql = "";
        Integer correcto = 1;
        sql = "INSERT INTO PERFIL VALUES (nextval('perfilseq'), upper('" + this.nombrePerfil + "'))";
        try {
            jdbcTemplate.execute(sql);
        } catch (DataAccessException e) {
            System.out.println(e.getMessage());
            correcto = 0;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Este Perfil ya Existe", "Ingrese otro perfil"));
        }
        if (correcto == 1) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Perfil Ingresado", " "));
            entrar("ROLE_PASO");
            numperfil(nombrePerfil);
        }

    }

    public void numperfil(String s) {
        String sql = "";
        sql = "select idperfil from perfil where upper(nombreperfil) = upper('" + s + "')";
        perfilSeleccionado = jdbcTemplate.queryForInt(sql);
    }
}
